package com.audi.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.audi.R;
import com.audi.dto.MemberItem;

public class ListHomeMemberAdapter extends BaseAdapter {
    private Activity mActivity;
    private ArrayList<MemberItem> mMemberList;

    public ListHomeMemberAdapter(Activity activity,
	    ArrayList<MemberItem> memberList) {
	mActivity = activity;
	mMemberList = memberList;
    }

    public int getCount() {
	return mMemberList.size();
    }

    public MemberItem getItem(int pos) {
	return mMemberList.get(pos);
    }

    public long getItemId(int pos) {
	return pos;
    }

    @SuppressLint("InflateParams")
    public View getView(int i, View convertView, ViewGroup viewgroup) {
	MemberItem item = mMemberList.get(i);
	ViewHolder viewHolder = null;
	if (convertView == null) {
	    viewHolder = new ViewHolder();
	    convertView = LayoutInflater.from(mActivity).inflate(
		    R.layout.row_home_member, null);
	    viewHolder.imvMemberIcon = (ImageView) convertView
		    .findViewById(R.id.imv_member_icon);
	    viewHolder.txtMemberName = (TextView) convertView
		    .findViewById(R.id.txt_member_name);
	    convertView.setTag(viewHolder);
	} else {
	    viewHolder = (ViewHolder) convertView.getTag();
	}
	// set Data for View
	if (TextUtils.isEmpty(item.getMemberPhoto())) {
	    viewHolder.imvMemberIcon
		    .setImageResource(R.drawable.member);
	} else {
	    BitmapDrawable bitmapDrawable = new BitmapDrawable(
		    mActivity.getResources(), item.getMemberPhoto());
	    viewHolder.imvMemberIcon.setImageDrawable(bitmapDrawable);
	}
	viewHolder.txtMemberName.setText(item.getMemberName());
	return convertView;
    }
    static class ViewHolder {
	public ImageView imvMemberIcon;
	public TextView txtMemberName;
    }
}
